import * as XLSX from "xlsx";
const ExportJsonExcel = require("js-export-excel");

const exportExcel = (headers, data, fileName = 'demo.xlsx') => {

    const _headers = headers
        .map((item, i) => Object.assign({}, { key: item.key, title: item.title, position: String.fromCharCode(65 + i) + 1 }))
        .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { key: next.key, v: next.title } }), {});
    const _data = data
        .map((item, i) => headers.map((key, j) => Object.assign({}, { content: item[key.key], position: String.fromCharCode(65 + j) + (i + 2) })))
        // 对刚才的结果进行降维处理（二维数组变成一维数组）
        .reduce((prev, next) => prev.concat(next))
        // 转换成 worksheet 需要的结构
        .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.content } }), {});

    // 合并 headers 和 data
    const output = Object.assign({}, _headers, _data);
    // 获取所有单元格的位置
    const outputPos = Object.keys(output);
    // 计算出范围 ,["A1",..., "H2"]
    const ref = `${outputPos[0]}:${outputPos[outputPos.length - 1]}`;

    // 构建 workbook 对象
    const wb = {
        SheetNames: ['mySheet'],
        Sheets: {
            mySheet: Object.assign(
                {},
                output,
                {
                    '!ref': ref,
                    '!cols': [{ wpx: 45 }, { wpx: 100 }, { wpx: 200 }, { wpx: 80 }, { wpx: 150 }, { wpx: 100 }, { wpx: 300 }, { wpx: 300 }],
                },
            ),
        },
    };
    // 导出 Excel
    XLSX.writeFile(wb, fileName);
}

const betterExportExcel = (headers, data, fileName = 'demo.xlsx') => { 
    var option = {};
    //文件名
    option.fileName = fileName;
    //第一列基本为“序号”列，故设定列宽短一些为3
    let sheetHeader = [], sheetFilter = [], columnWidths = [3];
    for (let i = 0; i < headers.length; i++) { 
        sheetHeader.push(headers[i].title);
        sheetFilter.push(headers[i].dataIndex);
        //固定了列宽，有需要可以调整
        if (i > 0) { 
            columnWidths.push(8)
        }
    }

    option.datas = [
        {
            sheetData: data,
            sheetName: fileName,
            sheetFilter: sheetFilter,
            sheetHeader: sheetHeader,
            columnWidths: columnWidths,
        }
    ];

    var toExcel = new ExportJsonExcel(option); //new
    toExcel.saveExcel(); //保存
}

export { exportExcel, betterExportExcel }

